package com.yanchi;


import com.yanchi.realm.CustomRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;

/**
 * 测试自定义 Realm
 */
public class TestCustom {
    public static void main(String[] args) {

        // 1、创建安全管理器对象
        DefaultSecurityManager securityManager = new DefaultSecurityManager();

        // 2、给安全管理器设置realm
        securityManager.setRealm(new CustomRealm());

        // 3、给全局安全管理器设置安全管理器
        SecurityUtils.setSecurityManager(securityManager);

        // 4、获取关键对象subject主体
        Subject subject = SecurityUtils.getSubject();

        // 5、创建令牌
        UsernamePasswordToken token = new UsernamePasswordToken("yanchi","123456");

        try {
            subject.login(token);
            System.out.println("登录成功！");
        }catch (UnknownAccountException e){
            System.out.println("登录失败：账号不存在");
            e.printStackTrace();
        }catch (IncorrectCredentialsException e){
            System.out.println("登录失败：密码错误");
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
